/**
 * 
 */
package base.ctrl;

import java.io.Serializable;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Model;

/**
 * 通用结果
 * 
 * @author suanp
 */
public class BaseResult<T> implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1637086074759518107L;

	private boolean isSuccess = false;

	private String message;

	private T data;

	public BaseResult() {
		super();
	}

	public BaseResult(boolean isSuccess) {
		super();
		this.isSuccess = isSuccess;
	}

	public BaseResult(boolean isSuccess, String message) {
		super();
		this.isSuccess = isSuccess;
		this.message = message;
	}

	public BaseResult(boolean isSuccess, String message, T data) {
		super();
		this.isSuccess = isSuccess;
		this.message = message;
		this.data = data;
	}

	public boolean isSuccess() {
		return isSuccess;
	}

	public BaseResult<T> setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public BaseResult<T> setMessage(String message) {
		this.message = message;
		return this;
	}

	public T getData() {
		return data;
	}

	public BaseResult<T> setData(T data) {
		this.data = data;
		return this;
	}

	public String toJsonString() {
		StringBuilder jsonSb = new StringBuilder("{\"isSuccess\":" + this.isSuccess);
		jsonSb.append(",\"message\":\"").append(message != null ? message : "").append("\"");
		jsonSb.append(",").append(toJsonString(data)).append("}");
		return jsonSb.toString();

	}

	@SuppressWarnings("rawtypes")
	private String toJsonString(Object data) {
		if (data != null && data instanceof Model) {
			return "\"data\":" + ((Model) data).toJson();
		}
		return "\"data\":" + JsonKit.toJson(data, 4);
	}

}
